package com.asset.salesforce.controller;

import java.util.List;

import javax.servlet.http.HttpSession;


import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.asset.salesforce.domain.Account;
import com.asset.salesforce.domain.Contact;
import com.asset.salesforce.domain.Employee;
import com.asset.salesforce.domain.Invoice;
import com.asset.salesforce.domain.InvoiceService;
import com.asset.salesforce.domain.InvoiceSku;
import com.asset.salesforce.domain.Lead;
import com.asset.salesforce.form.PdfForm;
import com.asset.salesforce.service.AccountService;
import com.asset.salesforce.service.CalendarService;
import com.asset.salesforce.service.ContactService;
import com.asset.salesforce.service.InvoiceOperationService;
import com.asset.salesforce.service.JoinRequestService;
import com.asset.salesforce.service.LeadService;
import com.asset.salesforce.service.ViewQueryService;

@Controller
public class ReportController {

	@Autowired JoinRequestService joinRequestService;
	
	@Autowired
	ViewQueryService queryService;
	
	@Autowired
	private InvoiceOperationService invoiceOperationService;
	
	@Autowired
	private ContactService contactService;
	
	@Autowired
	private LeadService leadService;
	
	@Autowired
	private AccountService accountService;
	
	@Autowired
	private JoinRequestService joinRequestService2;
	
	@Autowired
	private CalendarService calendarService;
	
	@RequestMapping(value="/report",method=RequestMethod.GET)
	public String getReport(Model model,HttpSession session){
		if(null ==session.getAttribute("userDetails") ){
			return "redirect:/s/login.html";
		}
		
		try {
			Employee userDetails = (Employee)session.getAttribute("userDetails");
			model.addAttribute("userDetails",userDetails);
			joinRequestService.getJoinRquestNo(session, model);
			//View view = new View();
			//queryService.getViewQueries(view.getId())
			
			return "report";
			
		} catch (Exception e) {
			// TODO: handle exception
			joinRequestService.getJoinRquestNo(session, model);
			calendarService.getAppointmentEventsNo(session, model);
			calendarService.getCallEventssNo(session, model);
			return "home";
		}
		
	}
	    @SuppressWarnings("unused")
		@RequestMapping(value = "/pdfView", method = RequestMethod.GET)
	    public String doSalesReportPDF(ModelAndView modelAndView,Model model,HttpSession session,
	    		@RequestParam(value="id",required=false)Integer id) 
			 {
	    	if (SecurityUtils.getSubject().hasRole("invoiceExport")){
	    		Employee empId = (Employee)session.getAttribute("userDetails");
				Invoice invoice = invoiceOperationService.getInvoiceById(id);
				//List<InvoiceItem> invoiceItems = invoiceOperationService.getInvoiceItemsByInvoice(id);
				List<InvoiceSku> invoiceSkus = invoiceOperationService.getInvoiceSkusByInvoice(invoice.getId());
				List<InvoiceService> invoiceServices = invoiceOperationService.getInvoiceServicesByInvoice(invoice.getId());
				
				String parentType=invoice.getContact().getViewType();
				Integer contactId = invoice.getContact().getId();
			    
			    Contact contact = invoice.getContact();
//			    Account account = accountService.getAccount(contact.getParentId());
			    Lead lead = leadService.getLeadById(contact.getParentId());
				
				if(contact!=null){
					model.addAttribute("contact",contact);
				}
				else{
					contact = new Contact();
					model.addAttribute("contact",contact);
				}
				if(lead!=null){
					model.addAttribute("lead",lead);
				}
//			    if(account!=null){
//			    	model.addAttribute("account",account);
//			    }
				PdfForm sales = new PdfForm();
				
				model.addAttribute("sales",sales);
				model.addAttribute("invoiceSkus", invoiceSkus);
				model.addAttribute("invoiceServices", invoiceServices);
				model.addAttribute("invoice", invoice);
				joinRequestService.getJoinRquestNo(session, model);
				calendarService.getAppointmentEventsNo(session, model);
				calendarService.getCallEventssNo(session, model);
			return "pdfView";
	    	}
	    	else{
	    		return "accessDenied";
	    	}
		}
}
